
<style lang="less" scoped>
  .page__wrapper {
    width: 100%;
    height: 100%;
  }
</style>

<template>
  <div class="page__wrapper" @keydown.enter="handleDataSearch">
    <simple-table :table-column="tableColumn" :table-data="tableData" :table-page-info="tablePageInfo" :table-data-count="tableDataTotalCount" @page-info-change="handlePageInfoChange" show-summary :summary-method="handleSummaryMethod">
      <template slot="searchSection">
        <simple-form v-model="searchModel" :form-field="searchFormField">
          <template slot="tableOperation">
            <el-button @click="handleDataSearch">{{$t('operation.search')}}</el-button>
            <el-button v-if="searchModel.companyid" @click="handleBack" type="primary">返回</el-button>
          </template>
        </simple-form>
      </template>
      <template slot-scope="scope" slot="uploadquarter">
        {{ $getDictLabel({type: '季度', value: scope.row.uploadquarter}) }}
      </template>
      <template slot-scope="scope" slot="orgname">
        <el-tag v-if="scope.row.companytype !== 'org_type_3'" style="cursor:pointer" @click="handleOrgnameClick(scope.row)">{{scope.row.orgname}}</el-tag>
        <template v-else>{{scope.row.orgname}}</template>
      </template>
      <template slot-scope="scope" slot="companytype1">
        {{ $getDictLabel({type: '公司性质', value: scope.row.companytype1}) }}
      </template>
      <template slot-scope="scope" slot="propertytype">
        {{ $getDictLabel({type: '产权性质_厂办', value: scope.row.propertytype}) }}
      </template>
      <template slot-scope="scope" slot="isconfirmation">
        {{ $getDictLabel({type: '是否', value: scope.row.isconfirmation}) }}
      </template>
      <template slot-scope="scope" slot="businessstatus">
        {{ $getDictLabel({type: '经营状态_厂办', value: scope.row.businessstatus}) }}
      </template>
      <template slot-scope="scope" slot="reformmethod">
        {{ $getDictLabel({type: '改革方式_厂办', value: scope.row.reformmethod}) }}
      </template>
      <template slot-scope="scope" slot="isrestructuring">
        {{ $getDictLabel({type: '是否', value: scope.row.isrestructuring}) }}
      </template>
      <template slot-scope="scope" slot="iscompletereform">
        {{ $getDictLabel({type: '是否', value: scope.row.iscompletereform}) }}
      </template>
      <template slot-scope="scope" slot="isinsolvent">
        {{ $getDictLabel({type: '是否', value: scope.row.isinsolvent}) }}
      </template>
      <template slot-scope="scope" slot="profitloss">
        {{ $getDictLabel({type: '盈亏情况', value: scope.row.profitloss}) }}
      </template>
    </simple-table>
  </div>
</template>
<script>
import Axios from 'axios'
/** 混入分页信息及数据总数信息 */
import pageMixins from '@/components/base/simple-table/page-mixins'
const BASEURL = {
  list: `/web/statistic/ajax.html?sys_method=GetList&sys_objName=V_Statistic_Factory_Collective&sys_tableName=V_Statistic_Factory_Collective`,
  summary: `/web/statistic/ajax.html?sys_method=GetList&sys_objName=V_Statistic_Factory_Collective_Count&sys_tableName=V_Statistic_Factory_Collective_Count`,
  getInitConfig: '/Web/SysAdmin/ajax.html?sys_method=GetInitConfig',
  getCombobox: '/Web/SysAdmin/ajax.html?sys_method=GetCombobox'
}
export default {
  name: 'statistic_factory',
  mixins: [pageMixins],
  data () {
    return {
      searchModel: {
        uploadyear: this.$moment().format('YYYY'),
        uploadquarter: String(this.$moment().quarter())
      },
      /** 表格数据 */
      tableData: [],
      summaryData: {},
      actionList: [],
      sys_mid: '',
      orgList: []
    }
  },
  computed: {
    searchFormField () {
      return [
        // Todo: 高级查询暂时未加企业名称 需要与后台去人谁去做数据过滤
        { prop: 'uploadyear', label: this.$t('balance_factory.uploadyear'), type: 'Datepicker', component: { clearable: false, type: 'year', valueFormat: 'yyyy' } },
        { prop: 'uploadquarter', label: this.$t('balance_factory.uploadquarter'), type: 'Select', component: { clearable: false, optionList: this.$getDictList('季度') } },
        { slotName: 'tableOperation', col: { span: 12, style: { textAlign: 'right' } }, labelWidth: '0' }
      ]
    },
    tableColumn () {
      return [
        { prop: 'orgname', label: '企业名称', slotName: 'orgname', width: 200, fixed: 'left' },
        { prop: 'uploadyear', label: this.$t('balance_factory.uploadyear'), width: 50, fixed: 'left' },
        { prop: 'uploadquarter', label: this.$t('balance_factory.uploadquarter'), slotName: 'uploadquarter', width: 70, fixed: 'left' },

        // 基本情况
        { label: this.$t('balance_factory.basicSituation'),
          children: [
            { prop: 'hostcompany', label: this.$t('balance_factory.hostcompany'), width: 120 },
            { label: this.$t('balance_factory.domicile'),
              children: [
                { prop: 'province', label: this.$t('balance_factory.province'), width: 100 },
                { prop: 'citycounty', label: this.$t('balance_factory.citycounty'), width: 100 }
              ]
            },
            { prop: 'companytype1', label: this.$t('balance_factory.companytype'), slotName: 'companytype1', width: 70 },
            { prop: 'propertytype', label: this.$t('balance_factory.propertytype'), slotName: 'propertytype', width: 100 },
            { prop: 'isconfirmation', label: this.$t('balance_factory.isconfirmation'), slotName: 'isconfirmation', width: 70 },
            { prop: 'registeredcapital', label: this.$t('balance_factory.registeredcapital'), width: 100 },
            { prop: 'equitystructure', label: this.$t('balance_factory.equitystructure'), width: 70 },
            { prop: 'managementlevel', label: this.$t('balance_factory.managementlevel'), width: 70 },
            { prop: 'businessstatus', label: this.$t('balance_factory.businessstatus'), slotName: 'businessstatus', width: 70 },
            { prop: 'mainbusiness', label: this.$t('balance_factory.mainbusiness'), width: 70 },
            { prop: 'insurancelocation', label: this.$t('balance_factory.insurancelocation'), width: 270 }
          ]
        },

        // 改革推进情况
        { label: this.$t('balance_factory.progressOfReform'),
          children: [
            { prop: 'reformmethod', label: this.$t('balance_factory.reformmethod'), slotName: 'reformmethod', width: 70 },
            { label: this.$t('balance_factory.theCostOfTheReformsThatHaveOccurred'),
              children: [
                { prop: 'reformcost', label: this.$t('balance_factory.reformcost'), width: 100 },
                { prop: 'assetdebtloss', label: this.$t('balance_factory.assetdebtloss'), width: 120 },
                { prop: 'otherfee', label: this.$t('balance_factory.otherfee'), width: 190 },
                { prop: 'resettlementcost', label: this.$t('balance_factory.resettlementcost'), width: 120 }
              ]
            },
            { prop: 'isrestructuring', label: this.$t('balance_factory.isrestructuring'), slotName: 'isrestructuring', width: 130 },
            { prop: 'iscompletereform', label: this.$t('balance_factory.iscompletereform'), slotName: 'iscompletereform', width: 110 },
            { prop: 'reformmeasuresprogress', label: this.$t('balance_factory.reformmeasuresprogress'), width: 110 }
          ]
        },

        // 资产负债情况
        { label: this.$t('balance_factory.balanceSheet'),
          children: [
            { label: this.$t('balance_factory.theEndOfTheSemester'),
              children: [
                { prop: 'totalassets', label: this.$t('balance_factory.totalassets'), width: 70 },
                { prop: 'netreceivables', label: this.$t('balance_factory.netreceivables'), width: 170 },
                { prop: 'netfixedassets', label: this.$t('balance_factory.netfixedassets'), width: 205 },
                { prop: 'totalliabilities', label: this.$t('balance_factory.totalliabilities'), width: 70 },
                { prop: 'corporateaccountspayable', label: this.$t('balance_factory.corporateaccountspayable'), width: 125 },
                { prop: 'netassets', label: this.$t('balance_factory.netassets'), width: 70 },
                { prop: 'assetsliabilities', label: this.$t('balance_factory.assetsliabilities'), width: 90 },
                { prop: 'isinsolvent', label: this.$t('balance_factory.isinsolvent'), slotName: 'isinsolvent', width: 100 }
              ]
            }

          ]
        },

        // 经营情况
        { label: this.$t('balance_factory.operatingCondition'),
          children: [
            { label: this.$t('balance_factory.thisYearTheCumulative'),
              children: [
                { label: this.$t('balance_factory.operatingIncome'), // 营业收入
                  children: [
                    { prop: 'totalincome', label: this.$t('balance_factory.totalincome'), width: 70 },
                    { prop: 'businessincome', label: this.$t('balance_factory.businessincome'), width: 190 }
                  ]
                },
                { label: this.$t('balance_factory.theCost'), // 成本费用
                  children: [
                    { prop: 'totalcost', label: this.$t('balance_factory.totalcost'), width: 70 },
                    { prop: 'laborcost', label: this.$t('balance_factory.laborcost'), width: 105 },
                    { prop: 'interestexpense', label: this.$t('balance_factory.interestexpense'), width: 70 },
                    { prop: 'depreciationdepletion', label: this.$t('balance_factory.depreciationdepletion'), width: 70 },
                    { prop: 'assetimpairmentloss', label: this.$t('balance_factory.assetimpairmentloss'), width: 95 }
                  ]
                },
                { prop: 'totalprofit', label: this.$t('balance_factory.totalprofit'), width: 70 },
                { prop: 'profitloss', label: this.$t('balance_factory.profitloss'), slotName: 'profitloss', width: 70 }
              ]
            }
          ]
        },

        // 人员情况
        { label: this.$t('balance_factory.theStaff'),
          children: [
            { prop: 'totalemployment', label: this.$t('balance_factory.totalemployment'), width: 100 },
            { label: this.$t('balance_factory.employees'), // 在职员工
              children: [
                { prop: 'totalemployees', label: this.$t('balance_factory.totalemployees'), width: 100 },
                { label: this.$t('balance_factory.sentByTheHostStateOwnedEnterprise'), // 主办国有企业派出
                  children: [
                    { prop: 'sponsorssendsubtotals', label: this.$t('balance_factory.sponsorssendsubtotals'), width: 120 },
                    { prop: 'contractedemployee', label: this.$t('balance_factory.contractedemployee'), width: 85 },
                    { prop: 'marketemployee', label: this.$t('balance_factory.marketemployee'), width: 85 }
                  ]
                },
                { label: this.$t('balance_factory.collectiveEnterpriseEmployeeTitle'), // 集体企业员工
                  children: [
                    { prop: 'collectiveenterpriseemployee', label: this.$t('balance_factory.collectiveenterpriseemployee'), width: 120 },
                    { prop: 'scope1', label: this.$t('balance_factory.scope1'), width: 195 },
                    { prop: 'scope2', label: this.$t('balance_factory.scope2'), width: 200 },
                    { prop: 'scope3', label: this.$t('balance_factory.scope3'), width: 200 },
                    { prop: 'scope4', label: this.$t('balance_factory.scope4'), width: 140 }
                  ]
                },
                { prop: 'laboremployeecurrent', label: this.$t('balance_factory.laboremployeecurrent'), width: 70 }
              ]
            },
            { label: this.$t('balance_factory.retirees'), // 离退休人员
              children: [
                { prop: 'totalretirees', label: this.$t('balance_factory.totalretirees'), width: 110 },
                { prop: 'yeartotalretirees', label: this.$t('balance_factory.yeartotalretirees'), width: 160 }
              ]
            }
          ]
        },

        // 本年累计在职人员变动情况
        { label: this.$t('balance_factory.theChangeOfTheInServicePersonnelAccumulativelyThisYear'),
          children: [
            { label: this.$t('balance_factory.attrition'), // 减员
              children: [
                { prop: 'subtotalreduction', label: this.$t('balance_factory.subtotalreduction'), width: 70 },
                { prop: 'naturalreduction', label: this.$t('balance_factory.naturalreduction'), width: 70 },
                { prop: 'agreementcancellation', label: this.$t('balance_factory.agreementcancellation'), width: 120 },
                { prop: 'earlyretirement', label: this.$t('balance_factory.earlyretirement'), width: 70 },
                { prop: 'otherreduction', label: this.$t('balance_factory.otherreduction'), width: 70 }
              ]
            },
            { label: this.$t('balance_factory.increase'), // 增员
              children: [
                { prop: 'subtotalincrease', label: this.$t('balance_factory.subtotalincrease'), width: 70 },
                { prop: 'mainbusinessemployee', label: this.$t('balance_factory.mainbusinessemployee'), width: 95 },
                { prop: 'recruitmentmarketemployee', label: this.$t('balance_factory.recruitmentmarketemployee'), width: 120 },
                { prop: 'laboremployeeincrease', label: this.$t('balance_factory.laboremployeeincrease'), width: 70 },
                { prop: 'otherincease', label: this.$t('balance_factory.otherincease'), width: 70 }
              ]
            }
          ]
        },

        // 人员分流安置情况
        { label: this.$t('balance_factory.personnelDiversionAndResettlement'),
          children: [
            { label: this.$t('balance_factory.resettlementNumber'), // 分流安置人数（包括协议解除劳动合同、提前退休及以下形式）
              children: [
                { prop: 'totalstaffing', label: this.$t('balance_factory.totalstaffing'), width: 290 },
                { prop: 'internalretirement', label: this.$t('balance_factory.internalretirement'), width: 70 },
                { prop: 'leavejob', label: this.$t('balance_factory.leavejob'), width: 70 },
                { prop: 'agreementreservation', label: this.$t('balance_factory.agreementreservation'), width: 120 },
                { prop: 'othertype', label: this.$t('balance_factory.othertype'), width: 70 },
                { prop: 'othertypedesc', label: this.$t('balance_factory.othertypedesc'), width: 95 }
              ]
            },
            { prop: 'cumulativedefault', label: this.$t('balance_factory.cumulativedefault'), width: 210 }
          ]
        }
      ]
    }
  },
  methods: {
    handleDataSearch () {
      let params = Object.assign({}, this.tablePageInfo, { sys_mid: this.sys_mid }, { sys_search: this.searchModel })

      let loadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })
      Axios.all([
        this.$axios.post(BASEURL.list, this.$_.cloneDeep(params)),
        this.$axios.post(BASEURL.summary, this.$_.cloneDeep(params))
      ])
        .then(Axios.spread((listResp, countResp) => {
          this.tableData = listResp.rows
          this.tableDataTotalCount = listResp.total

          this.summaryData = countResp.rows[0] || {}
        }))
        .finally(() => {
          loadingFlag.close()
        })
    },
    handleInitData () {
      let configLoadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })
      this.$axios.post(BASEURL.getInitConfig)
        .then(resp => {
          this.actionList = resp.actionKeys.split(',')
          this.sys_mid = resp.mid

          /** 设置列表及涉及主数据请求参数 */
          let params = Object.assign({}, this.tablePageInfo, { sys_mid: resp.mid }, { sys_search: this.searchModel })
          let comboParams = { sys_json: JSON.stringify([{ ObjName: 'C_SYS_Role' }, { ObjName: 'C_SYS_Org' }]) }
          let loadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })

          Axios.all([
            this.$axios.post(BASEURL.getCombobox, comboParams),
            this.$axios.post(BASEURL.list, this.$_.cloneDeep(params)),
            this.$axios.post(BASEURL.summary, this.$_.cloneDeep(params))
          ])
            .then(Axios.spread((comboData, listResp, countResp) => {
              this.orgList = comboData.C_SYS_Org

              this.tableData = listResp.rows
              this.tableDataTotalCount = listResp.total

              this.summaryData = countResp.rows[0] || {}
            }))
            .finally(() => {
              loadingFlag.close()
            })
        })
        .finally(() => {
          configLoadingFlag.close()
        })
    },
    handleOrgnameClick (row) {
      this.searchModel.companyid = row.orgid
      this.handleDataSearch()
    },
    handleBack () {
      delete this.searchModel.companyid
      this.handleDataSearch()
    },
    handleSummaryMethod (param) {
      let { columns } = param
      let sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index < 3) {
          // 年份、季度显示 -
          sums[index] = '-'
        } else {
          sums[index] = this.summaryData[column.property]
        }
      })
      return sums
    }
  },
  mounted () {
    this.handleInitData()
  }
}
</script>
